
//
// 120328 - AH - (Garage) Created.
// 120329 - AH - (Midtun) Sets Orb's CHAT_INPUT attribute.
// 120907 - AH - create(ConversationElement) now throw NullPointerException
//               if passed null for ce.
//

package org.soekrat.server.servlets.chat;

import java.util.List;

import org.soekrat.environment.Orb;
import org.soekrat.environment.RequestedSemanticEffects;
import org.soekrat.environment.RequestedSemanticEffects.RequestedSemanticEffect;
import org.soekrat.server.io.db.ConversationElement;
import org.soekrat.server.io.db.SemanticEffect;
import org.soekrat.server.io.db.SoekratDAO;
import org.soekrat.server.io.db.SoekratDAOFactory;

/**
 * <p>
 *   Factory object that created an
 *   {@link Orb} from a 
 *   {@link ConversationElement}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class OrbFactory 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public OrbFactory(){}
  
  /**
   * <p>
   *   Creates an 
   *   {@link Orb}.
   * </p>
   * @param ce {@link ConversationElement} to create from.
   * @return Created.
   */
  public Orb create(ConversationElement ce)
  {
    if (ce==null)
    {
      throw new NullPointerException("ce");  
    }
    
    Orb orb=new Orb();
    
    ce.setAttributes(orb);
    
    RequestedSemanticEffects effects=getSemanticEffects(ce);
    orb.setAttribute("semantic-effects", effects);
    
    return orb;
  }
  
  // Moved.
  private RequestedSemanticEffects getSemanticEffects(ConversationElement ce)
  {
    SoekratDAO dao=new SoekratDAOFactory().create();
    
    RequestedSemanticEffects retVal=new RequestedSemanticEffects();
    
    List<SemanticEffect> effects=dao.getSemanticEffects(ce);
    
    for (SemanticEffect curr : effects) 
    {
      String name=curr.getName();
      
      RequestedSemanticEffect rse=retVal.create(name);
      rse.setData("action", curr.getParameter("action"));
    }
    
    return retVal;
  }
  
}
